package com.huanzhidadi.jsoup;

import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;

public class JsoupHeaderDemo {
    public static void main(String[] args) throws IOException {
        // 1、爬取的目标url
        String targetUrl = "http://blog.huanzhidadi.com";
        // 2、获取connection
        Connection connection = Jsoup.connect(targetUrl);
        // 3、伪造请求头 浏览器F12调试中查看
        connection.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
        connection.header("Accept-Encoding", "gzip, deflate");
        connection.header("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
        connection.header("Cache-Control", "max-age=0");
        connection.header("Connection", "keep-alive");
        connection.header("Cookie", "csrf_token=b583f64d00c0422f821f6a3ad61816ab; __tins__19731447=%7B%22sid%22%3A%201691396418061%2C%20%22vd%22%3A%201%2C%20%22expires%22%3A%201691398218061%7D; __51cke__=; __51laig__=1");
        connection.header("Host", "blog.huanzhidadi.com");
        connection.header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36 Edg/115.0.1901.188");
        connection.ignoreHttpErrors(true);
        // 4、执行
        Connection.Response response = connection.method(Connection.Method.GET).execute();
        // 5、获取爬取结果
        String body = response.body();
        // System.out.println(body);
        // 6、获取Document
        Document document = response.parse();
        Element bodyElement = document.body();
        System.out.println(bodyElement);

        // 7、获取文章视图
        Elements articleElements = bodyElement.select(".list-article-box");
        for (Element articleElement : articleElements) {
            Elements titleElements = articleElement.select(".article-title");
            String title = titleElements.text();
            System.out.println(title);
        }

    }
}
